package cz.muni.fi.pa165.soccergamesarchive.validators;

import cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler;
import cz.muni.fi.pa165.soccergamesarchive.persistencelayer.entities.Team;

import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.INCOMPLETE_RECORD_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.INCORRECT_ID_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.NO_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.errorhandling.ErrorHandler.RUNTIME_ERROR;
import static cz.muni.fi.pa165.soccergamesarchive.validators.ValidatorTest.CORRECT_RECORD_TEST;
import static cz.muni.fi.pa165.soccergamesarchive.validators.ValidatorTest.FULL_TEST;
import static cz.muni.fi.pa165.soccergamesarchive.validators.ValidatorTest.ID_TEST;
import static cz.muni.fi.pa165.soccergamesarchive.validators.ValidatorTest.NULL_TEST;

/**
 *
 * @author Martin Fabian
 */
public class TeamValidator {

    public static int validateTeam(Team team, ValidatorTest... tests) {
        int testResult = NO_ERROR;

        for (ValidatorTest test : tests) {

            if (testResult != NO_ERROR) {
                return testResult;
            }

            switch (test) {
                case NULL_TEST: testResult = executeNullTest(team);
                    break;
                case ID_TEST: testResult = executeIDTest(team);
                    break;
                case CORRECT_RECORD_TEST: testResult = executeCorrectRecordTest(
                            team);
                    break;
                case FULL_TEST: testResult = executeFullTest(team);
                    break;
                default:
                    System.err.println("Undefined test: " + test);
                    return RUNTIME_ERROR;
            }
        }

        return testResult;
    }

    private static int executeNullTest(Team team) {
        return (team == null ? ErrorHandler.NULL_VALUE_ERROR
                : ErrorHandler.NO_ERROR);
    }

    private static int executeIDTest(Team team) {
        if (team.getId() == null || team.getId() < 0) {
            return INCORRECT_ID_ERROR;
        }

        return NO_ERROR;
    }

    private static int executeCorrectRecordTest(Team team) {
        if ((team.getName() == null || team.getName().equals(""))
                || (team.getManagerName() == null || team.getManagerName()
                .equals(""))) {
            return INCOMPLETE_RECORD_ERROR;
        }

        return NO_ERROR;
    }

    private static int executeFullTest(Team team) {
        int testResult = executeNullTest(team);

        if (testResult == NO_ERROR) {
            testResult = executeIDTest(team);

            if (testResult == NO_ERROR) {
                testResult = executeCorrectRecordTest(team);
            }
        }

        return testResult;
    }

}
